<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/18
 * Time: 10:59
 */

namespace app\admin\controller;


use app\admin\model\HomeAdvertisement;
use app\admin\model\HomeIcon;
use app\admin\model\HomeImages;
use app\admin\model\HomeLabel;

class Home extends Base
{

    public function homeImagesList(){
        return $this->fetch();
    }

    public function homeImagesData($page,$limit){
        $data = HomeImages::page($page,$limit)->select()->toArray();
        $count = HomeImages::count();
        $result = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($result);
    }

    public function homeImageAdd(){
        if (request()->isPost()){
            $post=input('post.');
            $data = [
                'title'=>$post['title'],
                'image'=>$post['image'],
                'url'=>$post['url'],
                'sort'=>$post['sort'],
                'status'=>$post['status']
            ];
            $ret = HomeImages::create($data);
            if (!$ret){
                return json(['code'=>0,'msg'=>'保存失败']);
            }
            return json(['code'=>1,'msg'=>'保存成功']);
        }
        return $this->fetch();
    }

    public function homeImageEdit($id){
        if (request()->isPost()){
            $post = input('post.');
            $ide = $post['id'];
            $data = [
                'title'=>$post['title'],
                'image'=>$post['image'],
                'url'=>$post['url'],
                'sort'=>$post['sort'],
                'status'=>$post['status']
            ];
            $ret = HomeImages::update($data,['id'=>$ide]);
            if (!$ret){
                return json(['code'=>0,'msg'=>'保存失败']);
            }
            return json(['code'=>1,'msg'=>'保存成功']);
        }
        $data = HomeImages::where('id',$id)->find();
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function homeImageDel(){
        $id = input('post.id');
        $ret = HomeImages::destroy($id);
        if (!$ret){
            return json(['code'=>0,'msg'=>'删除失败']);
        }
        return json(['code'=>1,'msg'=>'删除成功']);
    }





    public function homeAdvertisement(){
        $data = HomeAdvertisement::where('id','in',[1,2,3])->select()->toArray();
        $this->assign('data1',$data[0]);
        $this->assign('data2',$data[1]);
        $this->assign('data3',$data[2]);
        return $this->fetch();
    }

    public function homeAdvertisementSave(){
        $post = input('post.');
        $ide = $post['id'];
        $data = [
            'title'=>$post['title'],
            'image'=>$post['image'],
            'url'  =>$post['url']
        ];
        $ret = HomeAdvertisement::update($data,['id'=>$ide]);
        if (!$ret){
            return json(['code'=>0,'msg'=>'保存失败']);
        }
        return json(['code'=>1,'msg'=>'保存成功']);
    }

    public function homeIcon(){
        return $this->fetch();
    }
    public function homeIconData($page,$limit){
        $data = HomeIcon::page($page,$limit)->select()->toArray();
        $count = HomeIcon::count();
        $result = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($result);
    }
    public function homeIconEdit($id){
        if (request()->isPost()){
            $post = input('post.');
            $ide = $post['id'];
            $data = [
                'title'=>$post['title'],
                'image'=>$post['image'],
                'url'=>$post['url'],
                'sort'=>$post['sort']
            ];
            $ret = HomeIcon::update($data,['id'=>$ide]);
            if (!$ret){
                return json(['code'=>0,'msg'=>'保存失败']);
            }
            return json(['code'=>1,'msg'=>'保存成功']);
        }
        $data = HomeIcon::where('id',$id)->find();
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function homeLabel(){
        return $this->fetch();
    }
    public function homeLabelData($page,$limit){
        $data = HomeLabel::page($page,$limit)->select()->toArray();
        $count = HomeLabel::count();
        $result = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($result);
    }
    public function homeLabelEdit($id){
        if (request()->isPost()){
            $post = input('post.');
            $ide = $post['id'];
            $data = [
                'title'=>$post['title'],
                'image'=>$post['image'],
                'image_selection'=>$post['image_selection'],
                'url'=>$post['url'],
                'sort'=>$post['sort']
            ];
            $ret = HomeLabel::update($data,['id'=>$ide]);
            if (!$ret){
                return json(['code'=>0,'msg'=>'保存失败']);
            }
            return json(['code'=>1,'msg'=>'保存成功']);
        }
        $data = HomeLabel::where('id',$id)->find();
        $this->assign('data',$data);
        return $this->fetch();
    }

}